package tobsn.locationcalendar;

import tobsn.locationcalendar.db.DBAdapter;
import tobsn.locationcalendar.db.DBOpenHelper;
import tobsn.locationcalendar.map.TobsnMapActivity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class NotificationListActivity extends ListActivity {

	public static final int CHOOSE_NOTIFICATION = 1234;

	private DBAdapter mDBAdapter;
	private Cursor mCursor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// We'll define a custom screen layout here (the one shown above), but
		// typically, you could just use the standard ListActivity layout.
		setContentView(R.layout.notificationlist);

		mDBAdapter = new DBAdapter(this);
		mDBAdapter.open();
		mCursor = mDBAdapter.fetchAllPON();
		startManagingCursor(mCursor);

		// Now create a new list adapter bound to the cursor.
		// SimpleListAdapter is designed for binding to a Cursor.
		ListAdapter adapter = new SimpleCursorAdapter(this, // Context.
				// Specify the row template to use
				R.layout.notificationlistitem,
				// Pass in the cursor to bind to.
				mCursor,
				// Array of cursor columns to bind to.
				new String[] { DBOpenHelper.NOTIFICATION_COLUMN_TEXT,
						DBOpenHelper.NOTIFICATION_COLUMN_LATITUDE,
						DBOpenHelper.NOTIFICATION_COLUMN_LONGITUDE },
				// Parallel array of which template objects to bind to those
				// columns.
				new int[] { R.id.itemText, R.id.itemLatitude,
						R.id.itemLongitude });

		// Bind to our new adapter.
		setListAdapter(adapter);
		mDBAdapter.close();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);

		Intent data = new Intent();
		data.putExtra(TobsnMapActivity.EXTRA_NOTIFICATION_ID, id);
		setResult(CHOOSE_NOTIFICATION, data);
		finish();
	}

}
